'use strict';

const BaseController = require('./base');
const Controller = require('egg').Controller;

class MenuController extends BaseController {

  // 添加菜单/按钮
  async updateMenu() {
    const { ctx } = this;
    const menu = ctx.request.body;
    let updateData;
    // 筛选不需要更新的字段
    if (menu.type === 1) {
      updateData = {
        id: menu.id,
        title: menu.title,
        parentId: menu.parentId || null,
        url: menu.url,
        order: menu.order,
        permission: menu.permission
      }
    } else {
      updateData = {
        id: menu.id,
        title: menu.title,
        permission: menu.permission
      }
    }
    const result = await ctx.service.sys.menu.updateMenu(updateData);
    this.success(result);
  }

  // 添加菜单/按钮
  async addMenu() {
    const { ctx } = this;
    const postData = ctx.request.body;
    const result = await ctx.service.sys.menu.addMenu(postData);
    this.success(result);
  }

  // 获取菜单树
  async getMenuTree() {
    const { ctx } = this;
    const result = await ctx.service.sys.menu.getMenuTree();
    this.success(result);
  }

  // 获取菜单树
  async getMenus() {
    const { ctx } = this;
    const result = await ctx.service.sys.menu.getMenus();
    this.success(result);
  }

  // 删除菜单
  async deleteMenu() {
    const { ctx } = this;
    const result = await ctx.service.sys.menu.deleteMenu(ctx.query.id);
    this.success(result);
  }

}

module.exports = MenuController;
